unit BW_CustomObj_Acc;

{$mode delphi}{$H+}

interface

uses
  BW_Access ,

  LW_CustObj,
  LW_Types  ;

type
  TColor    = UInt32;
  TColorRec = record
    case Integer of
      0 : (RGBA       : TColor);
      1 : (A, B, G, R : Byte  );
  end;

  TCoordSys     = (csWorld          ,
                   csObject         ,
                   csIcon           ,
                   csViewport       ) ;

  TDrawMode     = (dmCullFace       = LWDRAWMODE_CULLFACE  ,
                   dmOutline        = LWDRAWMODE_OUTLINE   ,
                   dmDepthTest      = LWDRAWMODE_DEPTHTEST ,
                   dmDepthWrite     = LWDRAWMODE_DEPTHWRITE) ;
  TDrawModes    = set of TDrawMode                         ;

  TLinePattern  = (lpSolid          ,
                   lpDot            ,
                   lpDash           ,
                   lpLongDot        ) ;

  TTextJustify  = (tjLeft           ,
                   tjCenter         ,
                   tjRight          ) ;

  TViewType     = (vRightOrLeft     ,
                   vTopOrBottom     ,
                   vBackOrFront     ,
                   vPerspective     ,
                   vLight           ,
                   vCamera          ,
                   vSchematic       ) ;

  TViewNumber   = Integer           ;

  { TCustomObjAccess }

  TCustomObjAccess = class(THandlerAccess)
  private
    FColor       : TColorRec   ;
    FCoordSys    : TCoordSys   ;
    FDrawMode    : TDrawModes  ;
    FLinePattern : TLinePattern;
    FTextJustify : TTextJustify;

    FLastCoord   : TLWDVector  ;

    function  ColorToFloat
              (      Color          : TColorRec          ) : TLWFVector4;
                                                             inline     ;

    function  GetViewType                                  : TViewType  ;
    function  GetViewNumber                                : TViewNumber;

    procedure SetColor
              (      AValue         : TColor             ) ;
    procedure SetDrawMode
              (      AValue         : TDrawModes         ) ;
    procedure SetLinePattern
              (      AValue         : TLinePattern       ) ;

    function  GetViewDir                                   : TLWDVector ;

  public
    property  ViewType    : TViewType    read  GetViewType   ;
    property  ViewNumber  : TViewNumber  read  GetViewNumber ;

    property  Color       : TColor       read  FColor.RGBA
                                         write SetColor      ;
    property  CoordSys    : TCoordSys    read  FCoordSys
                                         write FCoordSys     ;
    property  DrawMode    : TDrawModes   read  FDrawMode
                                         write SetDrawMode   ;
    property  LinePattern : TLinePattern read  FLinePattern
                                         write SetLinePattern;
    property  TextJustify : TTextJustify read  FTextJustify
                                         write FTextJustify  ;

    property  ViewDir     : TLWDVector   read  GetViewDir    ;

    procedure SetTexture
              (      Size           : Integer            ;
                     Image          : PByte              ) ;
    procedure SetUVs
              (      UV0, UV1,
                     UV2, UV3       : TLWDVector2        ) ;
    procedure SetCoordSysItem
              (      Item           : PLWItem            ) ;
    procedure SetPart
              (      Part           : UInt32             ) ;

    procedure DrawCircle
              (      Radius         : Double             ) ; overload;
    procedure DrawCircle
              (      Radius         : Double             ;
                     Center         : array of Double    ) ; overload;
    procedure DrawDisk
              (      Radius         : Double             ) ; overload;
    procedure DrawDisk
              (      Radius         : Double             ;
                     Center         : array of Double    ) ; overload;
    procedure DrawLine
              (const StartPos       ,
                     EndPos         : TLWDVector         ) ;
    procedure DrawLineTo
              (      Pos            : array of Double    ) ;
    procedure DrawPoint
              (      Pos            : array of Double    ) ;
    procedure DrawPolygon
              (      NumV           : UInt32             ;
                     Vertices       : array of TLWDVector) ;
    procedure DrawPolygonIndexed
              (      NumV           : UInt32             ;
                     Vertices       : array of TLWDVector;
                     Indices        : array of UInt32    ) ;
    procedure DrawQuadrangle
              (      V1, V2, V3, V4 : array of Double    ) ;
    procedure DrawText
              (const Text           : String             ) ; overload;
    procedure DrawText
              (const Text           : String             ;
                     Pos            : array of Double    ) ; overload;
    procedure DrawTriangle
              (      V1, V2, V3     : array of Double    ) ;
  end;

implementation

{ TCustomObjAccess }

function TCustomObjAccess.ColorToFloat
                          (      Color          : TColorRec          )
                                                : TLWFVector4        ;
begin
  Result[0] := Color.R / 255;
  Result[1] := Color.G / 255;
  Result[2] := Color.B / 255;
  Result[3] := Color.A / 255;
end;

function  TCustomObjAccess.GetViewType                            : TViewType  ;
begin
  Result := TViewType(PLWCustomObjAccess(FAccess).View);
end;

function  TCustomObjAccess.GetViewNumber                          : TViewNumber;
begin
  Result := TViewNumber(PLWCustomObjAccess(FAccess).View);
end;

procedure TCustomObjAccess.SetColor
                           (     AValue         : TColor             ) ;
begin
  FColor.RGBA := AValue;

  PLWCustomObjAccess(FAccess).SetColor   (
  PLWCustomObjAccess(FAccess).DispData,   ColorToFloat(FColor));
end;

procedure TCustomObjAccess.SetDrawMode
                           (     AValue         : TDrawModes         ) ;
var
  Index : TDrawMode ;
  Mode  : UInt32 = 0;
begin
  for Index in AValue do
    Mode := Mode or Integer(Index);

  PLWCustomObjAccess(FAccess).SetDrawMode(
  PLWCustomObjAccess(FAccess).DispData,   Mode);

  FDrawMode := AValue;
end;

procedure TCustomObjAccess.SetLinePattern
                           (     AValue         : TLinePattern       ) ;
begin
  PLWCustomObjAccess(FAccess).SetPattern (
  PLWCustomObjAccess(FAccess).DispData,   Integer(AValue));

  FLinePattern := AValue;
end;

function  TCustomObjAccess.GetViewDir                                  : TLWDVector;
begin
  Result := PLWCustomObjAccess(FAccess).ViewDir;
end;

procedure TCustomObjAccess.SetTexture
                           (      Size           : Integer            ;
                                  Image          : PByte              ) ;
begin
  PLWCustomObjAccess(FAccess).SetTexture (
  PLWCustomObjAccess(FAccess).DispData,   Size, Image);
end;

procedure TCustomObjAccess.SetUVs
                           (      UV0, UV1,
                                  UV2, UV3       : TLWDVector2        ) ;
begin
  PLWCustomObjAccess(FAccess).SetUVs     (
  PLWCustomObjAccess(FAccess).DispData,   UV0, UV1, UV2, UV3);
end;

procedure TCustomObjAccess.DrawPoint
                           (      Pos            : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Point      (
  PLWCustomObjAccess(FAccess).DispData,   Pos, Integer(FCoordSys));

  FLastCoord := Pos;
end;

procedure TCustomObjAccess.DrawLine
                           (const StartPos       ,
                                  EndPos         : TLWDVector         ) ;
begin
  PLWCustomObjAccess(FAccess).Line       (
  PLWCustomObjAccess(FAccess).DispData,   StartPos, EndPos, Integer(FCoordSys));

  FLastCoord := EndPos;
end;

procedure TCustomObjAccess.DrawLineTo
                           (      Pos            : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Line       (
  PLWCustomObjAccess(FAccess),            FLastCoord, Pos, Integer(FCoordSys));

  FLastCoord := Pos;
end;

procedure TCustomObjAccess.DrawTriangle
                           (      V1, V2, V3     : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Triangle   (
  PLWCustomObjAccess(FAccess),            V1, V2, V3, Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawQuadrangle
                           (      V1, V2, V3, V4 : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Quad       (
  PLWCustomObjAccess(FAccess).DispData,   V1, V2, V3, V4, Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawCircle
                           (      Radius         : Double             ) ;
begin
  PLWCustomObjAccess(FAccess).Circle     (
  PLWCustomObjAccess(FAccess).DispData,   FLastCoord, Radius, Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawCircle
                           (      Radius         : Double             ;
                                  Center         : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Circle     (
  PLWCustomObjAccess(FAccess).DispData,   Center, Radius, Integer(FCoordSys));

  FLastCoord := Center;
end;

procedure TCustomObjAccess.DrawText
                           (const Text           : String             ) ;
begin
  PLWCustomObjAccess(FAccess).Text       (
  PLWCustomObjAccess(FAccess).DispData,   FLastCoord           ,
                                          PChar(Text)          ,
                                          Integer(FTextJustify),
                                          Integer(FCoordSys)   ) ;
end;

procedure TCustomObjAccess.DrawText
                           (const Text           : String             ;
                                  Pos            : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Text       (
  PLWCustomObjAccess(FAccess).DispData,   Pos                  ,
                                          PChar(Text)          ,
                                          Integer(FTextJustify),
                                          Integer(FCoordSys)   ) ;

  FLastCoord := Pos;
end;

procedure TCustomObjAccess.SetCoordSysItem
                           (      Item           : PLWItem            ) ;
begin
  PLWCustomObjAccess(FAccess).SetCSysItem(
  PLWCustomObjAccess(FAccess).DispData,   Item);
end;

procedure TCustomObjAccess.DrawPolygon
                           (      NumV           : UInt32             ;
                                  Vertices       : array of TLWDVector) ;
begin
  PLWCustomObjAccess(FAccess).Polygon    (
  PLWCustomObjAccess(FAccess).DispData,   NumV, Vertices, Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawPolygonIndexed
                           (      NumV           : UInt32             ;
                                  Vertices       : array of TLWDVector;
                                  Indices        : array of UInt32    ) ;
begin
  PLWCustomObjAccess(FAccess).PolyIndexed(
  PLWCustomObjAccess(FAccess).DispData,   NumV              ,
                                          Indices           ,
                                          Vertices          ,
                                          Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawDisk
                           (      Radius         : Double             ) ;
begin
  PLWCustomObjAccess(FAccess).Disk       (
  PLWCustomObjAccess(FAccess).DispData,   FLastCoord        ,
                                          Radius            ,
                                          Integer(FCoordSys));
end;

procedure TCustomObjAccess.DrawDisk
                           (      Radius         : Double             ;
                                  Center         : array of Double    ) ;
begin
  PLWCustomObjAccess(FAccess).Disk       (
  PLWCustomObjAccess(FAccess).DispData,   Center, Radius, Integer(FCoordSys));

  FLastCoord := Center;
end;

procedure TCustomObjAccess.SetPart
                           (      Part           : UInt32             ) ;
begin
  PLWCustomObjAccess(FAccess).SetPart    (
  PLWCustomObjAccess(FAccess).DispData,   Part);
end;

end.

